import { AsyncStorage } from 'react-native';
import Request from "../utils/Request";
const STORAGE_KEY = "STORAGE_KEY_order";
export default {
    namespace: 'order',
    state: {
        loading: false,
        currentTab: 0,
        newOrderList: [],
        newOrderNextPageUrl: null,
        cancelOrderList: [],
        cancelOrderNextPageUrl: null,
        finishOrderList: [],
        finishOrderNextPageUrl: null,
        refundOrderList: [],
        refundOrderNextPageUrl: null,
        progressOrderList: [],
        progressOrderNextPageUrl: null,
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *changeCurrentTab({ payload: { currentTab } }, { call, put, select }) {
            const order = yield select(state => state.order);
            if (order.currentTab !== currentTab) {
                yield put({
                    type: 'save',
                    payload: { currentTab }
                });
                yield put({ type: 'fetchData' });
            }
        },
        *fetchData({ payload }, { call, put, select }) {
            const order = yield select(state => state.order);
            console.log('开始获取订单信息', order);
            switch (order.currentTab) {
                case 0:
                    {
                        yield put({ type: 'newOrderList' });
                    }
                    break;
                case 1:
                    {
                        yield put({ type: 'cancelOrderList' });
                    }
                    break;
                case 2:
                    {
                        yield put({ type: 'finishOrderList' });
                    }
                    break;
                case 4:
                    {
                        yield put({ type: 'refundOrderList' });
                    }
                    break;
                case 3:
                    {
                        yield put({ type: 'progressOrderList' });
                    }
                    break;
            }
        },
        *newOrderList({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            console.log('开始获取新订单列表', payload);
            const data = yield call(Request.get, Request.getApi().newOrder, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        newOrderNextPageUrl: data.data.next_page_url,
                        newOrderList: data.data.data
                    }
                });
            }
        },
        *cancelOrderList({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            console.log('开始获取取消单列表', payload);
            const data = yield call(Request.get, Request.getApi().cancelOrder, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        newOrderNextPageUrl: data.data.next_page_url,
                        cancelOrderList: data.data.data
                    }
                });
            }
        },
        *finishOrderList({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            console.log('开始获取完成单列表', payload);
            const data = yield call(Request.get, Request.getApi().finishOrder, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        finishOrderNextPageUrl: data.data.next_page_url,
                        finishOrderList: data.data.data
                    }
                });
            }
        },
        *refundOrderList({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            console.log('开始获取退款售后订单列表', payload);
            const data = yield call(Request.get, Request.getApi().refundOrder, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        refundOrderNextPageUrl: data.data.next_page_url,
                        refundOrderList: data.data.data
                    }
                });
            }
        },
        *progressOrderList({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            console.log('开始获取进行中订单列表', payload);
            const data = yield call(Request.get, Request.getApi().progressOrder, Object.assign({ store_id: auth.profile.id }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({
                    type: 'save',
                    payload: {
                        progressOrderNextPageUrl: data.data.next_page_url,
                        progressOrderList: data.data.data
                    }
                });
            }
        },
        *acceptOrder({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(Request.post, Request.getApi().changeNewOrderStatus, Object.assign({ store_id: auth.profile.id, issend: 2 }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({ type: 'order/fetchData' });
            }
        },
        *rejectOrder({ payload }, { call, put, select }) {
            const auth = yield select(state => state.auth);
            yield put({
                type: 'save',
                payload: { loading: true }
            });
            const data = yield call(Request.post, Request.getApi().changeNewOrderStatus, Object.assign({ store_id: auth.profile.id, issend: 8 }, payload));
            yield put({
                type: 'save',
                payload: { loading: false }
            });
            if (data) {
                yield put({ type: 'order/fetchData' });
            }
        },
        *loadMore({ payload }, { call, put, select }) {
        }
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
